package com.anrongtec.zcpt.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.DataStateAdapter;
import com.anrongtec.zcpt.adapter.GridViewAdapter;
import com.anrongtec.zcpt.model.StatementData;
import com.anrongtec.zcpt.model.demo.InstallApps;
import com.anrongtec.zcpt.ui.activity.DragItemActivity;
import com.anrongtec.zcpt.ui.activity.MyFocusActivity;
import com.anrongtec.zcpt.ui.activity.NotificationActivity;
import com.anrongtec.zcpt.ui.activity.ReportActivity;
import com.anrongtec.zcpt.ui.activity.SettingActivity;
import com.anrongtec.zcpt.weather.WeatherActivity;
import com.anrongtec.zcpt.widget.ScrollGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

@SuppressLint("ValidFragment")
public class MainFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;//设置页
    @BindView(R.id.ll_notification)
    LinearLayout llNotification;//通知通告
    @BindView(R.id.gv_all_apps)
    ScrollGridView gvAllApps;//常用应用
    @BindView(R.id.rl_weather)
    RelativeLayout rlWeather;//天气
    @BindView(R.id.ll_homepage_my_focus)
    LinearLayout llHomepageMyFocus;
    @BindView(R.id.lv_main_statement)
    ListView lvMainStatement;
    @BindView(R.id.ll_statistics_report_more)
    LinearLayout llStatisticsReportMore;

    private TextView tv;
    private String name;

    private List<InstallApps> mDatas;
    private GridViewAdapter adapter;
    private List<StatementData> stateData;

    public MainFragment() {
    }

    public MainFragment(String fName) {
        this.name = fName;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, view);
        initDatas();
        initView();
        return view;
    }

    private void initView() {
        adapter = new GridViewAdapter(getActivity(), mDatas);
        adapter.setListener(this);
        gvAllApps.setAdapter(adapter);
        gvAllApps.setOnItemClickListener(this);

        DataStateAdapter lvAdapter = new DataStateAdapter(getActivity(), stateData);
        lvMainStatement.setAdapter(lvAdapter);
    }

    @OnClick({R.id.ll_setting, R.id.ll_notification, R.id.rl_weather, R.id.ll_homepage_my_focus, R.id.ll_statistics_report_more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_setting://设置
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.ll_notification://通知
                startActivity(new Intent(getActivity(), NotificationActivity.class));
                break;
            case R.id.iv_item_drag_app://常用应用
                Intent intent = new Intent(getActivity(), DragItemActivity.class);
                startActivityForResult(intent, RESULT_OK);
                break;
            case R.id.rl_weather://天气
                startActivity(new Intent(getActivity(), WeatherActivity.class));
                break;
            case R.id.ll_homepage_my_focus://我的关注
                startActivity(new Intent(getActivity(), MyFocusActivity.class));
                break;
            case R.id.ll_statistics_report_more://统计报表更多
                startActivity(new Intent(getActivity(), ReportActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    private void initDatas() {
        mDatas = new ArrayList<>();
        InstallApps weichaapp = new InstallApps();
        weichaapp.setAppName("微察E勘查");
        weichaapp.setPackageName("com.hisign.csipadmini_free.activity");
        weichaapp.setImgUrl(R.drawable.jilin_weicha);
        InstallApps jingxinapp = new InstallApps();
        jingxinapp.setAppName("警信");
        jingxinapp.setPackageName("com.anrongtec.wx");
        jingxinapp.setImgUrl(R.drawable.jilin_jingxin);
        InstallApps jiaojingapp = new InstallApps();
        jiaojingapp.setAppName("吉林交警");
        jiaojingapp.setPackageName("com.anrongvs");
        jiaojingapp.setImgUrl(R.drawable.jilin_jiaojing);
        InstallApps anquanapp = new InstallApps();
        anquanapp.setAppName("安全客户端");
        anquanapp.setPackageName("com.hisign.csipadmini_free.activity");
        anquanapp.setImgUrl(R.drawable.jilin_safe_client);
        //新增演示
        InstallApps hechaapp = new InstallApps();
        hechaapp.setAppName("移动警务");
        hechaapp.setPackageName("com.anrongvs");
        hechaapp.setImgUrl(R.drawable.ic_hechahelu);
        InstallApps shequapp = new InstallApps();
        shequapp.setAppName("随手记");
        shequapp.setPackageName("com.lk.notes");
        shequapp.setImgUrl(R.drawable.ic_launcher1);

        mDatas.add(weichaapp);
        mDatas.add(jingxinapp);
        mDatas.add(jiaojingapp);
        mDatas.add(anquanapp);
        mDatas.add(hechaapp);
        mDatas.add(shequapp);

        stateData = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            StatementData data = new StatementData();
//            String[] averagePeople = {"","平均抓获人数","本月抓获"};
//            String[] averagePeoples = {"", "5"};
            String[] averagePeople = {"", "平均抓获人数", "本月抓获"};
            String[] averagePeoples = {"", "7", "8", "9"};
            String[] tablePeople = {"本辖区案发", "本月案发"};
            String[] tablePeoples = {"", "8"};

            SparseArray<String> sparsePeople1 = new SparseArray<>();
            sparsePeople1.put(4, "8");
            sparsePeople1.put(5, "8");
            SparseArray<String> sparsePeople2 = new SparseArray<>();
            sparsePeople2.put(4, "8");
            List<Integer> averageNumMonth = new ArrayList<>();
            List<String> NumMonth = new ArrayList<>();
            List<String> normalAverageMonth = new ArrayList<>();

            averageNumMonth.add(5);
            averageNumMonth.add(6);
            averageNumMonth.add(8);
            averageNumMonth.add(5);

            NumMonth.add("5");
            NumMonth.add("6");
            NumMonth.add("7");
            NumMonth.add("8");

            normalAverageMonth.add("5月");
            normalAverageMonth.add("6月");
            normalAverageMonth.add("7月");
            normalAverageMonth.add("8月");

            data.setAveragePeopleTitle("季度抓获人数");
            data.setTablePeopleTitle("案发人数");
            data.setAverageMonthPeople("平均每月核查人数");
            data.setAveragePeople(averagePeople);
            data.setAveragePeoples(averagePeoples);
            data.setTablePeople(tablePeople);
            data.setTablePeoples(tablePeoples);
            data.setSparsePeople1(sparsePeople1);
            data.setSparsePeople2(sparsePeople2);
            data.setAverageNumMonth(averageNumMonth);
            data.setNumMonth(NumMonth);
            data.setNormalAverageMonth(normalAverageMonth);
            stateData.add(data);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startAPP(mDatas.get(position).getPackageName());
//        if (isexit(getActivity(), mDatas.get(position).getPackageName())) {
//            Intent intent = new Intent();
//            // a应用启动b应用
//            ComponentName cn = new ComponentName("com.hisign.csipadmini_free.activity",
//                    "com.hisign.csipadmini_free.activity.LoginActivity");
//            intent.setComponent(cn);
//            Uri uri = Uri.parse("com.hisign.csipadmini_free.activity");// 此处应与B程序中Data中标签一致
//            intent.setData(uri);
//            startActivity(intent);
//        } else {
//            Toast.makeText(getActivity(), "应用未安装，请去商店下载", Toast.LENGTH_SHORT).show();
//        }

    }

    /*
* 启动一个app
*/
    public void startAPP(String appPackageName) {
        try {
            Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(appPackageName);
            if (intent != null)
                startActivity(intent);
            else
                Toast.makeText(getActivity(), "应用未安装，请去商店下载", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getActivity(), "应用未安装，请去商店下载", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 通过packagename判断应用是否安装
     *
     * @param context
     * @param
     * @return 跳转的应用主activity Intent
     */

    public static boolean isexit(Context context, String pk_name) {
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo("com.hisign.csipadmini_free.activity", 0);
            if (info != null) {
                return true;
            } else {
                return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}
